package main

import (
	"context"
	"errors"
	"fmt"

	"golang.org/x/sync/errgroup"
)

func ErrGroupDemo01() {
	ctx := context.Background()
	group, _ := errgroup.WithContext(ctx)
	nums := []int{-1, 0, 1}
	for _, num := range nums {
		tempNum := num

		//子协程
		group.Go(func() error {
			if tempNum < 0 {
				return errors.New("tempNum<0")
			}
			fmt.Println("tempNum:", tempNum)
			return nil
		})
	}

	if err := group.Wait(); err != nil {
		fmt.Println("Get errors: ", err)
	} else {
		fmt.Println("Gel all nums successfully!")
	}
}
